<!DOCTYPE html>
<html lang="zh-CN">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>用户登录</title>
    <style>
        * {
            margin: 0;
            padding: 0;
            box-sizing: border-box;
        }

        body {
            font-family: 'Segoe UI', Tahoma, Geneva, Verdana, sans-serif;
            background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
            height: 100vh;
            display: flex;
            justify-content: center;
            align-items: center;
        }

        .login-container {
            background: white;
            padding: 40px;
            border-radius: 10px;
            box-shadow: 0 15px 35px rgba(0, 0, 0, 0.1);
            width: 100%;
            max-width: 400px;
        }

        .login-header {
            text-align: center;
            margin-bottom: 30px;
        }

        .login-header h1 {
            color: #333;
            font-size: 28px;
            margin-bottom: 10px;
        }

        .login-header p {
            color: #666;
            font-size: 14px;
        }

        .form-group {
            margin-bottom: 20px;
        }

        .form-group label {
            display: block;
            margin-bottom: 5px;
            color: #333;
            font-weight: 500;
        }

        .form-group input {
            width: 100%;
            padding: 12px 15px;
            border: 2px solid #e1e5e9;
            border-radius: 6px;
            font-size: 16px;
            transition: all 0.3s ease;
        }

        .form-group input:focus {
            outline: none;
            border-color: #667eea;
            box-shadow: 0 0 0 3px rgba(102, 126, 234, 0.1);
        }

        .login-btn {
            width: 100%;
            padding: 12px;
            background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
            color: white;
            border: none;
            border-radius: 6px;
            font-size: 16px;
            font-weight: 600;
            cursor: pointer;
            transition: all 0.3s ease;
        }

        .login-btn:hover {
            transform: translateY(-2px);
            box-shadow: 0 5px 15px rgba(102, 126, 234, 0.4);
        }

        .login-btn:active {
            transform: translateY(0);
        }

        .error-message {
            color: #e74c3c;
            text-align: center;
            margin-top: 15px;
            padding: 10px;
            background: #fdf2f2;
            border: 1px solid #f5c6cb;
            border-radius: 4px;
            display: none;
        }

        .loading {
            display: none;
            text-align: center;
            margin-top: 10px;
        }

        .spinner {
            border: 3px solid #f3f3f3;
            border-top: 3px solid #667eea;
            border-radius: 50%;
            width: 20px;
            height: 20px;
            animation: spin 1s linear infinite;
            display: inline-block;
        }

        @keyframes spin {
            0% { transform: rotate(0deg); }
            100% { transform: rotate(360deg); }
        }
    </style>
</head>
<body>
<div class="login-container">
    <div class="login-header">
        <h1>欢迎登录</h1>
        <p>请输入您的用户名和密码</p>
    </div>

    <form id="loginForm">
        <div class="form-group">
            <label for="userName">用户名：</label>
            <input
                    type="text"
                    id="userName"
                    name="userName"
                    placeholder="请输入用户名"
                    required
                    autocomplete="username"
            >
        </div>

        <div class="form-group">
            <label for="password">密码：</label>
            <input
                    type="password"
                    id="password"
                    name="password"
                    placeholder="请输入密码"
                    required
                    autocomplete="current-password"
            >
        </div>

        <button type="submit" class="login-btn">登录</button>

        <div class="loading" id="loading">
            <div class="spinner"></div>
            <span style="margin-left: 10px;">登录中...</span>
        </div>

        <div class="error-message" id="errorMessage"></div>
    </form>
</div>

<script>
    document.getElementById('loginForm').addEventListener('submit', async function(e) {
        e.preventDefault();

        const userName = document.getElementById('userName').value.trim();
        const password = document.getElementById('password').value.trim();
        const errorMessage = document.getElementById('errorMessage');
        const loading = document.getElementById('loading');

        // 隐藏错误消息
        errorMessage.style.display = 'none';

        // 显示加载中
        loading.style.display = 'block';

        // 构建登录URL
        const loginUrl = `http://localhost:8080/login?userName=${encodeURIComponent(userName)}&password=${encodeURIComponent(password)}`;
        const resp =  await fetch(loginUrl)
        const re = await resp.json()

        if(re.success) {
            window.location.href = 'index.html';
        }else{
            alert(re.message)
        }


    });

    // 按Enter键快速提交
    document.addEventListener('keypress', function(e) {
        if (e.key === 'Enter') {
            const focused = document.activeElement;
            if (focused && (focused.type === 'text' || focused.type === 'password')) {
                document.getElementById('loginForm').dispatchEvent(new Event('submit'));
            }
        }
    });

    // 页面加载完成后自动聚焦到用户名输入框
    window.addEventListener('load', function() {
        document.getElementById('userName').focus();
    });
</script>
</body>
</html>